package com.zwr.sheller.customer.Service.Impl;

import com.zwr.sheller.core.common.WQQBusinessException;
import com.zwr.sheller.core.common.WQQExceptionCode;
import com.zwr.sheller.customer.Service.ItemService;
import com.zwr.sheller.customer.mapper.ItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: 18032
 * @Date: 9:26 2022/1/14
 */
@Service
public class ItemServiceImpl implements ItemService {
    @Autowired(required = false)
    ItemMapper itemMapper;

    @Override
    public List<Map<String, Object>> selectSort() throws WQQBusinessException {
        //首先查出一级分类 Todo 可以优化为递归
        List<Map<String, Object>> list = itemMapper.selectFirstSort();
        if (list == null){
            throw new WQQBusinessException(WQQExceptionCode.NO_ITEM);
        }
        for (Map<String, Object> item : list) {
            List<Map<String, Object>> selectSecondSort = selectSecondAndThirdSort(Long.valueOf(item.get("item_id").toString()));
            item.put("secondSort",selectSecondSort);
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> selectSecondAndThirdSort(Long itemId) throws WQQBusinessException {
        List<Map<String, Object>> secondList = itemMapper.selectSecondSort(itemId);
        if (secondList == null){
            throw  new WQQBusinessException(WQQExceptionCode.NO_ITEM);
        }
        for (Map<String, Object> secondItem : secondList) {
            List<Map<String, Object>> thirdList = itemMapper.selectThirdSort(Long.valueOf(secondItem.get("item_id").toString()));
            secondItem.put("thirdSort",thirdList);
        }
        return secondList;
    }
}
